package ModelLayer;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

public class LeaseRecords {

	private ArrayList<Lease> leaseRecords;
	
	private static LeaseRecords instance = null;
	
	private LeaseRecords()
	{
		
		leaseRecords = new ArrayList<Lease>();
	}

	public static LeaseRecords getInstance()
	{
	    if(instance == null)
	        {
	            instance = new LeaseRecords();
	        }
	    return instance;
	}
	
	//create and add an lease
	public int createLease(int customerId, int employeeId, long startDate, long endDate) throws RealException
	{
		Lease lease = new Lease(customerId, employeeId, startDate, endDate);
		leaseRecords.add(lease);
		return lease.getId();
	}
	
	// remove an lease
	public void removeLease(int id) throws RealException
	{
		Lease lease = getLease(id);
		lease.returnLeaseItemUnits();
		leaseRecords.remove(getLease(id));
	}
	
	//update information about lease
	public void updateLease(int id, int choise, String value) throws RealException
	{
		Lease lease;
		lease = getLease(id);
		switch (choise) {
			case 1:
				lease.setEmployee(EmployeeRecords.getInstance().getEmployee(Integer.parseInt(value)));
				break;
			case 2:
				lease.setCustomer(CustomerRecords.getInstance().getCustomer(Integer.parseInt(value)));
				break;
			case 3:
				lease.setStartDate(Long.parseLong(value));
				break;
			case 4:
				lease.setEndDate(Long.parseLong(value));
		}
	}
			
	public void returnLease(int id, String returnDate) throws NumberFormatException, RealException
	{
		getLease(id).returnLease(Long.parseLong(returnDate));
	}
							
	//return an instance with type A
	public Lease getLease(int id) throws RealException
	{
		Iterator<Lease> it = leaseRecords.iterator();
		Lease theLease = null;
		boolean found = false;
		while(it.hasNext() && !found)
		{
			Lease lease = it.next();
			if (id == lease.getId()) {
				return lease;
			}
		}
		if(!found) throw new RealException("Lease is not found");
		return theLease;
	}
	
	public ArrayList<Lease> getLeaseRecords()
	{
		return leaseRecords;
	}
	
	
	//print all Borrowed Lease information
	public String printAllBorrowedLeaseInfo()
	{
		String description = "";
		if (leaseRecords.size() != 0) {
			for (Lease lease: leaseRecords) {
				if (lease.getStatus() == Status.BorrowedLease)
					description = description + lease.toString(); 
			}
		}
		return description;
	}
	
	//print all returned Lease information
	public String printAllReturnedLeaseInfo()
	{
		String description = "";
		if (leaseRecords.size() != 0) {
			for (Lease lease: leaseRecords) {
				if (lease.getStatus() == Status.ReturnedLease || lease.getStatus() == Status.ReturnedLeaseExpired)
						description = description + lease.toString(); 
			}
		}
		return description;
	}
	
	@SuppressWarnings("unchecked")
	public void load(ObjectInputStream obj_in) throws IOException, ClassNotFoundException
	{
		this.leaseRecords = (ArrayList<Lease>) obj_in.readObject();
		Lease.setLastId(((Integer)obj_in.readObject()).intValue());
	}
	
	public void save(ObjectOutputStream obj_out) throws IOException
	{		
		obj_out.writeObject(leaseRecords);
		obj_out.writeObject(new Integer(Lease.getLastId()));
	}

	public ArrayList<Lease> getLeasesRecords() {
		return leaseRecords;
	}

	public ArrayList<Lease> getLeases() {
		return leaseRecords;
	}
	
	
}